import { HttpClient, HttpHeaders } from '@angular/common/http';
import { NzMessageService } from 'ng-zorro-antd';

declare var base: any;
declare var security: any;

// http协助类
export class HttpUtils {

    msg: NzMessageService;
    http: HttpClient;

    constructor(
        msg: NzMessageService,
        http: HttpClient) {
        this.msg = msg;
        this.http = http;
    }

    private errorOption(url, fail, er) {
        // 是登录地址，取消再操作
        if (url.indexOf('/currentuser/isLogin') > 0) {
            this.msg.info('数据访问失败!');
            return;
        }
        // 出错后，看是否处于登录状态
        security.SecurityDataService.isLogin(
            (data: any) => {
                if (data.url) {
                    // 没登录，则登录
                    this.toLogin(data);
                } else {
                    // 登录了，显示错误信息
                    this.msg.info('数据访问失败!');
                    if (fail) {
                        fail(er);
                    }
                }
            },
            () => {
                // 登录了，显示错误信息
                this.msg.info('数据访问失败!');
                if (fail) {
                    fail(er);
                }
            }
        );
    }

    private toLogin(data: any) {
        // 做登录转向
        let url = data.url;
        const paras = base.BaseService.getUrlParams();
        Object.keys(paras).forEach((key) => {
            if (key !== 'service' && key !== 'ticket') {
                url += '&' + key + '=' + paras[key];
            }
        });
        const child = base.BaseService.getChild();
        if (child) {
            url += '&_child=' + child;
        }
        window.location.href = url;
    }

    // 初始化mis api 参数
    initRemoteFun() {

        // get远程方法
        base.BaseService.get = (url: string, success, fail) => {
            const jwts = security.SecurityService.jwt;
            const obsData = this.http.get<any>(url, {
                headers: new HttpHeaders({ jwt: jwts })
            }).pipe();
            obsData.subscribe(
                (rd) => {
                    if (rd.code === '-1') {
                        if (success) {
                            success(rd.data);
                        }
                    } else {
                        this.msg.info('系统内部错误:' + rd.msg + '!');
                        if (fail) {
                            fail(rd);
                        }
                    }
                },
                (er) => {
                    this.errorOption(url, fail, er);
                }
            );
        };

        // post远程方法
        base.BaseService.post = (url: string, data: any, success, fail) => {
            const jwts = security.SecurityService.jwt;
            const obsData = this.http.post<any>(url, data, { headers: new HttpHeaders({ jwt: jwts }) })
                .pipe();
            obsData.subscribe(
                (rd) => {
                    if (rd.code === '-1') {
                        if (success) {
                            success(rd.data);
                        }
                    } else {
                        this.msg.info('系统内部错误:' + rd.msg + '!');
                        if (fail) {
                            fail(rd);
                        }
                    }
                },
                (er) => {
                    this.errorOption(url, fail, er);
                }
            );
        };

        // put远程方法
        base.BaseService.put = (url: string, data: any, success, fail) => {
        };

        // delete远程方法
        base.BaseService.delete = (url: string, success, fail) => {
            const jwts = security.SecurityService.jwt;
            const obsData = this.http.delete<any>(url, { headers: new HttpHeaders({ jwt: jwts }) })
                .pipe();
            obsData.subscribe(
                (rd) => {
                    if (rd.code === '-1') {
                        if (success) {
                            success(rd.data);
                        }
                    } else {
                        this.msg.info('系统内部错误:' + rd.msg + '!');
                        if (fail) {
                            fail(rd);
                        }
                    }
                },
                (er) => {
                    this.errorOption(url, fail, er);
                }
            );
        };

        // post远程方法
        base.BaseService.postSubscribe = (url: string, data: any, res) => {
            const jwts = security.SecurityService.jwt;
            this.http.post(url, data, {
                headers: new HttpHeaders({ jwt: jwts }),
                responseType: 'blob', observe: 'response'
            }).subscribe(rs => {
                if (res) {
                    res(rs);
                }
            });
        };
    }

}
